package test.thread;
/**
 * 并不会造成死锁
 */
public class YieldCauseDeadLock {
	private byte[] lock = new byte[0]; 
	
	public static void main(String[] args) {
		new YieldCauseDeadLock().test();
	}
	
	public void test(){
		new Thread(new MyThreadOne()).start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		new Thread(new MyThreadTwo()).start();
	}
	
	class MyThreadOne implements Runnable {

		@Override
		public void run() {
			synchronized (lock) {
				System.out.println("线程"+Thread.currentThread().getName()+"获取到了锁");
				Thread.yield();	
				System.out.println("线程"+Thread.currentThread().getName()+"调用了Thread.yield()");
				//模拟做事
				System.out.println("线程"+Thread.currentThread().getName()+"做事开始");
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("线程"+Thread.currentThread().getName()+"做事结束");
			}
			
		}
		
	}
	class MyThreadTwo implements Runnable {

		@Override
		public void run() {
			synchronized (lock) {
				System.out.println("线程"+Thread.currentThread().getName()+"获取到了锁");
			}
		}
		
	}
}
